package com.iking.housekeeping.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.iking.housekeeping.redis.RedisObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.servicestaff.ServiceStaff;
import com.iking.housekeeping.entity.user.SysUser;
import com.iking.housekeeping.httpbean.login.reqbean.LoginParam;
import com.iking.housekeeping.service.servicestaff.ServiceStaffService;
import com.iking.housekeeping.service.user.LoginService;
import com.iking.housekeeping.util.MessageDigest5;
import com.iking.housekeeping.util.token.WebTokenUtil;

/**
 * 系统登录
 * @author 张重阳
 *
 */
@RestController
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	@Autowired 
	private ServiceStaffService serviceStaffService;
	@Autowired
	private RedisObjectService<String> redisObjectString;
	/**
	 * 登录
	 * @param loginParam
	 * @param bindingResult
	 * @return
	 * @throws InterruptedException 
	 */
	@PostMapping(value = "")
	public ReturnCode login(@RequestBody @Valid LoginParam loginParam, BindingResult bindingResult) throws InterruptedException {
		//参数校验
		if (bindingResult.hasErrors()) {
			//用户名或密码不能为空
			return new ReturnCode(CodeEnum.LOGIN_PARAM_ERROR);
		}
		//根据用户名查找用户
		SysUser sysUser = loginService.selectUserByLoginName(loginParam.getfLoginName());
		if(sysUser == null) {
			return new ReturnCode(CodeEnum.FAIL, "该用户不存在", null);
		}
		//判断用户密码是否正确
		Boolean flag = loginService.checkPassword(sysUser, MessageDigest5.md5(loginParam.getfPassword()));
		//false则用户名或密码错误
		if(!flag){
			return new ReturnCode(CodeEnum.LOGIN_ERROR);
		}
		//生成用户webToken
		String webToken = WebTokenUtil.getUserWebToken(sysUser.getfId());
		//将用户webToken存入redis中
		loginService.saveWebToken(sysUser.getfId(), webToken);
		//获取webToken并存入redis
		return new ReturnCode(CodeEnum.SUCCESS, webToken);
	}
	/**
	 * 
	 * @Description:服务人员登录
	 * @author:CGZ
	 * @param loginParam
	 * @param bindingResult
	 * @return
	 * @throws InterruptedException
	 */
	@PostMapping(value = "/servicestaff")
	public ReturnCode loginServicestaff(@RequestBody @Valid LoginParam loginParam, BindingResult bindingResult) throws InterruptedException {
		//参数校验
		if (bindingResult.hasErrors()) {
			//用户名或密码不能为空
			return new ReturnCode(CodeEnum.LOGIN_PARAM_ERROR);
		}
		ServiceStaff serviceStaff = new ServiceStaff();
		serviceStaff.setfPhone(loginParam.getfLoginName());
		serviceStaff.setfPassword(loginParam.getfPassword());
		//根据用户名查找用户
		ServiceStaff serviceStaffs = serviceStaffService.selectRepeat(serviceStaff);
		if(serviceStaffs ==  null) {
			return new ReturnCode(CodeEnum.FAIL, "该服务人员不存在", null);
		}
		SysUser sysUser = new SysUser();
		sysUser.setfLoginName(serviceStaffs.getfPhone());
		sysUser.setfPassword(serviceStaffs.getfPassword());
		//判断用户密码是否正确
		Boolean flag = loginService.checkPassword(sysUser, MessageDigest5.md5(loginParam.getfPassword()));
		//false则用户名或密码错误
		if(!flag){
			return new ReturnCode(CodeEnum.LOGIN_ERROR);
		}
		//生成用户webToken
		String webToken = WebTokenUtil.getUserWebToken(serviceStaffs.getfId());
		//将用户webToken存入redis中
		loginService.saveWebToken(serviceStaffs.getfId(), webToken);
		//获取webToken并存入redis
		return new ReturnCode(CodeEnum.SUCCESS, webToken);
	}
	/**
	 * 退出登录
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/logout")
	public ReturnCode logout(HttpServletRequest request) {
		//获取用户Id
		String userId = WebTokenUtil.getUserIdByRequest(request);
		String redisWebToken = redisObjectString.get("webToken:" + userId);
		//判断WebToken是否失效
		if(redisWebToken!=null && !"".equals(redisWebToken)){
            //删除redis中的用户webToken
			loginService.deleteWebToken(userId);
		}
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	
}
